use std::path::Path;
use crate::execute::{net_execute, vc_execute};
use crate::vc::VcRedist;
use winreg::enums::{HKEY_LOCAL_MACHINE, KEY_READ};
use winreg::RegKey;
use crate::framework::FrameworkInfo;
use anyhow::Result;
use console::style;

#[derive(Debug)]
pub struct VcRuntimeVersion {
    vc2005: bool,
    vc2008: bool,
    vc2010: bool,
    vc2012: bool,
    vc2013: bool,
    vc2015_2019: bool,
}

impl VcRuntimeVersion {
    pub fn new() -> Self {
        Self {
            vc2005: false,
            vc2008: false,
            vc2010: false,
            vc2012: false,
            vc2013: false,
            vc2015_2019: false,
        }
    }

    pub fn update(&mut self, ty: &str, value: bool) {
        match ty {
            "vc2005" => self.vc2005 = value,
            "vc2008" => self.vc2008 = value,
            "vc2010" => self.vc2010 = value,
            "vc2012" => self.vc2012 = value,
            "vc2013" => self.vc2013 = value,
            "vc2015_2019" => self.vc2015_2019 = value,
            _ => {}
        }
    }

}

pub async fn vc_detection() -> Result<()> {
    println!("{} 启动检测VC Runtime安装....", style("ℹ️").blue());
    let mut version = VcRuntimeVersion::new();
    let hkcr = RegKey::predef(HKEY_LOCAL_MACHINE);
    if let Ok(uninstall) = hkcr.open_subkey_with_flags(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", KEY_READ) {
        for key in uninstall.enum_keys().filter_map(|key| key.ok()) {
            if let Ok(uninstall_subkey) = uninstall.open_subkey(key) {
                if let Ok(display_name) = uninstall_subkey.get_value::<String, _>("DisplayName") {
                    if display_name.contains("Microsoft Visual C++") {
                        let v = display_name.split_ascii_whitespace().nth(3).unwrap();
                        if v == "2022" {
                            version.update("vc2015_2019", true);
                        }else {
                            version.update(&format!("vc{}", v), true);
                        }
                    }
                }
            }
        }
    }

    // if !version.vc2005 {
    //     vc_execute(VcRedist::VcRedist2005X86)?;
    // }else {
    //     println!("系统已安装VC Runtime 2005!");
    // }
    if !version.vc2008 {
        vc_execute(VcRedist::VcRedist2008X86).await?;
        vc_execute(VcRedist::VcRedist2008X64).await?;
    }else {
        println!("{} 系统已安装VC Runtime 2008!", style("✅").green());
    }

    if !version.vc2010 {
        vc_execute(VcRedist::VcRedist2010X86).await?;
        vc_execute(VcRedist::VcRedist2010X64).await?;
    }else {
        println!("{} 系统已安装VC Runtime 2010!", style("✅").green());
    }

    if !version.vc2012 {
        vc_execute(VcRedist::VcRedist2012X86).await?;
        vc_execute(VcRedist::VcRedist2012X64).await?;
    }else {
        println!("{} 系统已安装VC Runtime 2012!", style("✅").green());
    }

    if !version.vc2013 {
        vc_execute(VcRedist::VcRedist2013X86).await?;
        vc_execute(VcRedist::VcRedist2013X64).await?;
    }else {
        println!("{} 系统已安装VC Runtime 2013!", style("✅").green());
    }

    if !version.vc2015_2019 {
        vc_execute(VcRedist::VcRedist20152019X86).await?;
        vc_execute(VcRedist::VcRedist20152019X64).await?;
    }else {
        println!("{} 系统已安装VC Runtime 2015_2019!", style("✅").green());
    }
    Ok(())
}
/// .Net Framework 检测和安装
pub async fn net_detection() -> Result<()> {
    println!("{} 启动检测.Net Framework 4安装...", style("ℹ️").blue());
    let fw = FrameworkInfo::new();
    let net_file = Path::new(fw.check_file);
    let hkcr = RegKey::predef(HKEY_LOCAL_MACHINE);
    let check: bool = hkcr.open_subkey_with_flags(fw.client_reg, KEY_READ).is_ok()
        && hkcr.open_subkey_with_flags(fw.full_reg, KEY_READ).is_ok()
        && net_file.exists() && net_file.is_file();
    // 根据check值判断安装
    if !check {
        net_execute(fw).await?;
    }else {
        println!("{} 系统已安装.Net Framework 4!", style("✅").green());
    }

    Ok(())
}